<template>

  <scroller :maxHeight="80">
  <div class="file-list">
    <div class="file-list table" v-if="mode == ViewMode.Table" fit="true">
      <el-table :data="filteredFiles">
        <el-table-column label="名称" width="250">
          <template #default="scope">
            <span v-if="scope.row.isdir">
              <ri class="dir-icon" i="folder"></ri>
              <a :href="fileURL(scope.row)"><strong>{{scope.row.name}}</strong></a>
            </span>
            <span v-else>
              <ri class="file-icon" i="file"></ri>
              <a :href="fileURL(scope.row)">{{scope.row.name}}</a>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="intro" label="简介"></el-table-column>
        <el-table-column label="修改时间" width="200">
          <template #default="scope">
            <span style="margin-left: 10px"> {{ timeAgo(scope.row.modtime) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-else class="file-list simple">
    <div v-for="(f, i) in filteredFiles" :key="i" class="file-entry" :class="f.isdir ? 'dir' : 'file'">
      <a :href="fileURL(f)"> <!-- :href="'/#/materials/file/'+dir+'/'+f.name">-->
        {{f.name}}
      </a>
    </div>
    </div>
  </div>
  </scroller>
</template>


<script>
import paths from "@/js/paths.js";
import Scroller from "@/tools/Scroller.vue";

const ViewMode = {
  Simple: 0,
  Table: 1,
};

export default {
  components: {
    Scroller
  },
  props: ["dir", "showHidden"],
  data() {
    return {
      mode: ViewMode.Table,
      ViewMode: ViewMode,
      files: [],
    };
  },
  watch: {
    // 因为都在同一个主URL（即/materials）之下，只能通过watch的方式来监测URL变化
    dir() {
      this.loadFiles();
    },
  },
  created() {
    this.loadFiles();
  },
  computed: {
    filteredFiles() {
      if (this.showHidden) {
        return this.files;
      } else {
        return this.files.filter(f => !f.name.startsWith("."));
      }
    }
  },
  methods: {
    fileURL(f) {
      let p = this.$route.path;
      let middle = f.isdir ? "tree/master" : "blob/master";
      if (this.$route.params.nodetype !== undefined) {
        p = paths.join("/#/", p, f.name);
      } else {
        p = paths.join("/#/", p, middle, f.name);
      }
      return p;
    },
    loadFiles() {
      if (this.dir) {
        console.log("this.dir: ", this.dir);
        this.$axios.get(paths.join("/api/v1/materials/ls/", this.dir)).then((res) => {
          let files = res.data;
          files.sort((a, b) => b.isdir - a.isdir)
          this.files = files;
        });
      }
    },
    timeAgo(time) {
      return window.moment(time).fromNow();
    }
  }
};
</script>

<style lang="stylus">
.file-list
  padding 1vh 1vw
  text-align left
  align-items left

  &.simple
    column-count 3

    .file-entry
      padding 5px
    .dir
      font-weight bold

  &.table
    padding 0

    .dir-icon,.file-icon
      margin-right 0.5vw
      color #409EFF

    a
      color #666

</style>